<?php

declare(strict_types=1);

namespace App\Controller\Admin;


use App\Controller\AbstractController;
use App\Model\ChargeOption;
use App\Model\Device;
use App\Model\Normal;
use App\Services\Iot\IotService1;
use App\Services\Iot\IotService1 as IotService;
use App\Services\JSSDK;
use App\Services\Wechat;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Redis\Redis;
use Phper666\JWTAuth\JWT;
use Psr\Http\Message\ResponseInterface;
use Trait\ResponseMsg;


class ProSetController extends AbstractController
{
    use ResponseMsg;

    protected Redis $redis;
    protected Normal $normal;
    protected JWT $jwt;
    protected Wechat $wechat;

    public function __construct()
    {
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);
        $this->wechat = container()->get(Wechat::class);
    }

   public function getParams(RequestInterface $request):ResponseInterface{
        $jssdk = new JSSDK();
        $uri = $request->input('uri');
        $params = $jssdk->getSignPackage($uri);
        return $this->success($params);
   }

    public function getDeviceParams(RequestInterface $request):ResponseInterface{
        $sn = $request->input('sn');
        $device = Device::where('sn',$sn)->first();
        return $this->success($device);
    }

    public function updateParams(RequestInterface $request):ResponseInterface{
        $powerValues = $request->input('powerValues');
        try{
            if(!is_array($powerValues)){
                $powerValues = json_decode($powerValues);
            }
            $device = Device::where('sn',$powerValues['sn'])->first();
            if($device->distributor_id != $powerValues['distributor_id']){
                $powerValues['child_distributor_id'] = 0;
            }
            $device->update($powerValues);
            $device->updateParams();
            return $this->success(msg:'更新成功 请3-5秒后重载参数查看');
        }catch (\Exception $e){
            return $this->error(1001,$e->getMessage());
        }
    }

    /**开启指定端口
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function startPort(RequestInterface $request):ResponseInterface{
        $iot_sn = $request->input('iot_sn');
        $fee = $request->input('fee');
        $minutes= $request->input('minutes');
        $port = $request->input('port');
        $device = Device::where('iot_sn',$iot_sn)->first();
        if($device->is_new == 1){
            $charge = ChargeOption::where('distributor_id',0)
                ->where('is_del',1)
                ->first();
            IotService::startNew($device->iot_sn, $port, $charge, $fee,$minutes,100);
        }else{
            IotService1::start($iot_sn,$port,$minutes,$fee);
        }
        return $this->success(msg:'开启端口成功');
    }

    public function updateUrl(RequestInterface $request):ResponseInterface{
        $imei = $request->input('iot_sn');
        if(!$imei){
            return $this->error(msg:'请先填写物联网编号');
        }
        $path = $request->input('path','/device/eBIKE_ML307R_Upgrade_v1.01.004.bin');
        $url = $request->input('url','http://yimits.oss-cn-beijing.aliyuncs.com:80');
        IotService1::updateUrl($imei,$path,$url);
        return $this->success(msg:'固件升级中.. 请稍后留意设备查看');
    }

    public function endPort(RequestInterface $request):ResponseInterface{
        $iot_sn = $request->input('iot_sn');
        $port = $request->input('port');
        IotService1::stop($iot_sn,$port);
        return $this->success(msg:'开启关闭已下发');
    }
}
